home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_01 / allison / atox.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-02  |  542 b   |  31 lines

  1. LISTING 1 - Converts a hex-string to a number in ASCII environments
  2.  
  3. #include <ctype.h>
  4. #include <assert.h>
  5.  
  6. long atox(char *s)
  7. {
  8.      long sum;
  9.  
  10.      assert(s);
  11.  
  12.      /* Skip whitespace */
  13.      while (isspace(*s))
  14.           ++s;
  15.  
  16.      /* Do the conversion */
  17.      for (sum = 0L; isxdigit(*s); ++s)
  18.      {
  19.           int digit;
  20.  
  21.           if (isdigit(*s))
  22.                digit = *s - '0';
  23.           else
  24.                digit = toupper(*s) - 'A' + 10;
  25.           sum = sum*16L + digit;
  26.      }
  27.  
  28.      return sum;
  29. }
  30.  
  31.